package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.List;
@CrossOrigin
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("findUser")
    public List<User> findUser(){
        return userService.findAll();
    }
    //http://localhost:9000/findById?id=1
    @GetMapping("findById")
    public User findById(Integer id){
        return userService.findById(id);
    }
    //http://localhost:9000/findUserByNA?name="xxx"&age=xx
    @GetMapping("findUserByNA")
    public User findUserByNA(User user){
        return userService.findUserByNA(user);
    }
    //http://localhost:9000/findUserLike?name=xx
    @GetMapping("findUserLike")
    public List<User> findUserLike(String name){
        return userService.findUserLike(name);
    }
    //http://localhost:9000/findUserByIds?ids=1,3,5,6,7
    @GetMapping("findUserByIds")
    public List<User> findUserByIds(int[] ids){
        return userService.findUserByIds(ids);
    }
    //http://localhost:9000/findUser2?id=xx&name=“xx”&age=xx&sex=xx
    @GetMapping("findUser2")
    public List<User> findUser2(User user){
        return userService.findUser2(user);
    }
    //URL:http://localhost:9000/user/貂蝉/18/227
    //解析: URL:http://localhost:9000/user/{name}/{age}/{id}
//    @GetMapping("user/{name}/{age}/{id}")
//    public String updateUser(@PathVariable String name,
//                           @PathVariable Integer age,
//                           @PathVariable Integer id){
//        User user = new User();
//        user.setName(name).setAge(age).setId(id);
//        userService.updateUser(user);
//        return "已更新";
//    }
    @GetMapping("user/{name}/{age}/{id}")//restful:如果参数名称与对象中的属性名称一致，可以直接用对象接收
    public String updateUser(User user){
        userService.updateUser(user);
        return "已更新";
    }


}
